﻿Imports System.ComponentModel.DataAnnotations
Imports Models.Users

Public Class Groups
    Inherits BaseService

    Sub New(request As System.Net.Http.HttpRequestMessage)
        Me.loadToken(request)
    End Sub
    Sub New(token As String)
        Me.token = token
    End Sub

    ''' <summary>
    ''' Gets the specified id.
    ''' </summary>
    ''' <param name="id">The id.</param>
    ''' <returns></returns>
    Public Function [Get](id As Integer) As GroupVO
        Try
            Me.checkAccess("GETGROUP")
            Dim oEntity As GroupVO

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                Dim oDataEntity As Data.BaseModel.Group = (From u In oDB.Groups Where u.GroupId = id Select u).FirstOrDefault()

                If oDataEntity IsNot Nothing Then
                    oEntity = New GroupVO(oDataEntity)
                End If
            End Using

            Return oEntity

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try


    End Function


    ''' <summary>
    ''' Get List of Groups
    ''' </summary>
    ''' <param name="companyId">optional for filter</param>
    ''' <returns>Array of Groups</returns>
    ''' <remarks></remarks>
    Public Function List(Optional companyId As Integer = -1) As IEnumerable(Of GroupVO)
        Try
            Me.checkAccess("LISTGROUPS")
            Dim oResult As New Generic.List(Of GroupVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Group)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                If Not companyId = -1 Then
                    oDataEntity = (From c In oDB.Groups Where c.CompanyId = companyId Select c).ToList

                Else
                    oDataEntity = oDB.Groups.ToList()

                End If

                If oDataEntity IsNot Nothing Then
                    For Each dbData As Data.BaseModel.Group In oDataEntity
                        oResult.Add(New GroupVO(dbData, True))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Update a solution
    ''' </summary>
    ''' <param name="group"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Update(group As GroupVO) As Integer
        Try
            Me.checkAccess("UPDATEGROUP")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oDataSolution As Data.BaseModel.Group = (From c In oDB.Groups Where c.GroupId = group.groupId Select c).FirstOrDefault()

                If oDataSolution IsNot Nothing Then

                    If Not String.IsNullOrEmpty(group.name) Then
                        oDataSolution.Name = group.name
                    End If
                    If Not String.IsNullOrEmpty(group.code) Then
                        oDataSolution.Code = group.code
                    End If

                    nRes = oDB.SaveChanges()

                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' Create a solution
    ''' </summary>
    ''' <param name="group"></param>
    ''' <returns>The new ID  of the entity</returns>
    ''' <remarks></remarks>
    Public Function Create(group As GroupVO) As Integer
        Try
            Me.checkAccess("CREATEGROUP")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As New Data.BaseModel.Group()
                If oData IsNot Nothing Then
                    If Not String.IsNullOrEmpty(group.name) Then
                        oData.Name = group.name
                    End If
                    If Not String.IsNullOrEmpty(group.code) Then
                        oData.Code = group.code
                    End If

                    oDB.Groups.AddObject(oData)
                End If
                If oDB.SaveChanges() > 0 Then
                    nRes = oData.GroupId
                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' Delete a Solution
    ''' </summary>
    ''' <param name="id">SolutionId</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Delete(id As Integer) As Integer
        Try
            Me.checkAccess("DELETEGROUP")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As Data.BaseModel.Group = (From c In oDB.Groups Where c.GroupId = id Select c).FirstOrDefault()

                If oData IsNot Nothing Then
                    oDB.DeleteObject(oData)
                    nRes = oDB.SaveChanges()
                End If

            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

End Class